<?php

namespace app\admin\controller;
use app\common\controller\Backend;
use app\admin\model\Admin;
use app\common\model\UserGroup;
use think\Db;
/**
 * 微信用户管理
 *
 * @icon   fa fa-list
 * @remark 用于管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
 */
class Member extends Backend
{
	protected $noNeedLogin = [''];
    protected $noNeedRight = [''];

    public function _initialize()
    {
        parent::_initialize();
        //移除HTML标签
        $this->model = model('app\common\model\Members');
    }
    public function index(){
    	if ($this->request->isAjax()) {
    		$map=[];
    		$map['wechat_id'] = $this->wechat_id;
    		list($where, $sort, $order, $offset, $limit) = $this->buildparams('nickname');
    		$list = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->paginate($limit);
			$result = array("total" => $list->total(), "rows" => $list->items());
			return json($result);
    	}
        $groups = UserGroup::where(['wechat_id'=>$this->wechat_id])->column("name","group_id");
        $this->assign('groups',is_array($groups) ? $groups : []);
    	return $this->view->fetch();
    }
    public function reload($ids=null){
        $ids = explode(",", $ids);
        $ids = is_array($ids) ? $ids : [];
        $members = $this->model->where(['id'=>['in',$ids]])->select();
        if($members){
            if($members){
                $weobj = $this->weobj();
                foreach($members as $member){
                    if(empty($member->openid)){
                        continue;
                    }
                    $user = $weobj->getUserInfo($member->openid);
                    if($user){
                        $user['tagid_list'] = isset($user['tagid_list']) && is_array($user['tagid_list']) ? json_encode($user['tagid_list']) : '';
                        $user['update_at'] = time();
                        $result = $this->model->where(['id'=>$member->id])->update($user);
                    }
                }
                $this->success("处理完成");
            }
        }
        $this->error("处理失败");
    }
    public function group($ids=null,$params=null){
        if(intval($params) > 0 && !empty($ids)){
            $ids = explode(",", $ids);
            $ids = is_array($ids) ? $ids : [];
            $members = $this->model->where(['id'=>['in',$ids]])->select();
            if($members){
                $weobj = $this->weobj();
                foreach($members as $member){
                    if(empty($member->openid)){
                        continue;
                    }
                    Db::startTrans();
                    try{
                        $member->groupid = $params;
                        $member->save();
                        $result = $weobj->updateGroupMembers($params,$member->openid);
                        if(!$result){
                            Db::rollback();
                            continue;
                        }
                        Db::commit();
                    }catch (\Exception $e){
                        Db::rollback();
                    }
                }
            }
            $this->success("处理完成");
        }
        $this->error("处理失败");
    }
    // 下载所有的用户
    public function download($next_openid=null){
        $weobj = $this->weobj();
        $items = $weobj->getUserList($next_openid);
        if(isset($items['count']) && $items['count'] > 0){
            \fast\Async::hook('members',['wechat_id'=>$this->wechat_id,'openids'=>$items['data']['openid']]);
            if(!empty($items['next_openid'])){
                $this->download($items['next_openid']); 
                return ''; 
            }
        }
        $this->success('下载成功，供'.$items['total'].'条记录');
    }
}